package com.dnzx.service.impl;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.dnzx.framework.orm.mybatis.MyBatisBaseServiceImpl;
import org.dnzx.framework.orm.mybatis.MybatisBaseDao;
import org.dnzx.framework.page.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import com.dnzx.dao.IUserDao;
import com.dnzx.model.SmsBO;
import com.dnzx.model.system.UserBO;
import com.dnzx.security.JWTUserDetails;
import com.dnzx.service.IParamService;
import com.dnzx.service.ISmsService;
import com.dnzx.service.IUserService;


/**
 * <p>系统后台用户service实现类 </p>
 *
 * @author Steven
 * @version V1.0
 * @Package com.dnzx.service.impl
 * @date 2017/9/15 10:54
 */
@Service
public class UserServiceImpl extends MyBatisBaseServiceImpl<UserBO, Long> implements IUserService {
	@Resource
    private IUserDao userDao;
	
	@Autowired
    private ISmsService smsService;
	@Autowired
    private IParamService paramService;
	
    @Override
    public MybatisBaseDao<UserBO, Long> getBaseDAO() {
        return this.userDao;
    }

    @Override
    public void save(UserBO userBO) throws Exception {
        userBO.setStatus("1");
        this.userDao.save(userBO);
    }
    
    @Override
    public void delete(Long id) throws Exception {
        this.userDao.delete(id);
    }

    @Override
    public void update(UserBO userBO) throws Exception {
//    	UserBO userBODb = this.userDao.get(userBO.getId());
//    	// 校验手机号
//    	if (!userBODb.getMobile().equals(userBO.getMobile())) {// 手机号不与原来的一致，则表示手机号有更改，需要校验手机号是否跟其他重名
//    		UserBO userBOQuery = new UserBO();
//    		userBOQuery.setMobile(userBO.getMobile());
//        	int count = this.userDao.queryCount(userBOQuery);
//        	if (count > 0) {
//        		throw new Exception("已存在该手机号的用户,请重新输入");
//        	}
//    	}
//    	// 校验身份证号
//    	if (!userBODb.getIdCard().equals(userBO.getIdCard())) {// 手机号不与原来的一致，则表示手机号有更改，需要校验手机号是否跟其他重名
//    		UserBO userBOQuery = new UserBO();
//    		userBOQuery.setIdCard(userBO.getIdCard());
//        	int count = this.userDao.queryCount(userBO);
//        	if (count > 0) {
//        		throw new Exception("已存在该身份证号码的用户,请重新输入");
//        	}
//    	}
        this.userDao.update(userBO);
    }
    
    @Override
    public void updatePwd(UserBO userBO, String langCode) throws Exception {
    	Authentication authen = SecurityContextHolder.getContext().getAuthentication();
        JWTUserDetails userDetails = (JWTUserDetails)authen.getPrincipal();
        userBO.setId(userDetails.getUserId());
    	UserBO userBODb = this.get(userBO.getId());
    	BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
    	if (!encoder.matches(userBO.getPassword(), userBODb.getPassword())) {
    		String message = this.paramService.get("PASSWORD_ERROR_" + langCode);// 原密码错误
    		throw new Exception(message);
    	}
    	
    	UserBO userBOQuery = new UserBO();
    	userBOQuery.setMobile(userBODb.getMobile());
    	List<UserBO> userBOList = this.queryList(userBOQuery);
    	for (UserBO userBODbTemp : userBOList) {
    		userBODbTemp.setPassword(encoder.encode(userBO.getNewPassword()));
            this.userDao.update(userBODbTemp);
    	}
    }
    
    @Override
    public void forgetPwd(UserBO userBO) throws Exception {
    	// 需要校验短信验证码是否有效
    	if (userBO.getEmail() == null || "".equals(userBO.getEmail())) {
    		throw new Exception("邮箱为空");
    	}
    	
    	List<UserBO> userBOList = this.queryList(userBO);
    	if (userBOList == null || userBOList.size() <= 0) {
    		throw new Exception("用户不存在");
    	}
        SmsBO smsBO = new SmsBO();
        smsBO.setUserId(userBO.getId());
        smsBO.setCode(userBO.getCode());
        if (!this.smsService.checkCode(smsBO)) {
        	throw new Exception("验证码错误或验证码已过期");
        }
    	BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
    	System.out.println("userBO.getPassword() >>>>> " + userBO.getPassword());
    	for (UserBO userBODb : userBOList) {
    		userBODb.setPassword(encoder.encode(userBO.getPassword()));
            this.userDao.update(userBODb);
    	}
    }
    
    @Override
    public UserBO get(Long id) {
    	UserBO userBO = this.userDao.get(id);
    	return userBO;
    }
    
    @Override
    public UserBO get(UserBO userBO) {
    	List<UserBO> userBOList = this.userDao.queryList(userBO);
    	if (userBOList != null && userBOList.size() > 0) {
    		return userBOList.get(0);
    	}
    	return null;
    }
    
    @Override
    public List<UserBO> queryList(UserBO userBO) {
    	return this.userDao.queryList(userBO);
    }

    @Override
    public PageResult<List<UserBO>> queryPage(UserBO userBO, PageResult<List<UserBO>> pageResult) {
        int totalRows = this.userDao.queryCount(userBO);
        pageResult.setTotalRows(totalRows);
        pageResult.setCurrentPage(userBO.getCurrentPage());
        pageResult.setPageSize(userBO.getPageSize());
		List<UserBO> records = this.userDao.queryPage(pageResult.getPageSize(), pageResult.getStartRow(), userBO);
		pageResult.setT(records);
		return pageResult;
    }
    
}
